package core.cwm;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import core.obj.Object;
import core.sub.Subject;

public class PRRecord {
	private Map<Subject, PRObject> subjectPRData;
	static private PRRecord prRecords = null;
	
	private PRRecord() {
		subjectPRData = new HashMap<Subject, PRObject>();
	}
	
	public static PRRecord getInstance() {
		if(prRecords == null)
			prRecords = new PRRecord();
		return prRecords;
	}
	
	public PRObject getPRObjects(Subject subject) {
		if(subjectPRData.containsKey(subject))
			return subjectPRData.get(subject); 
		return null;
	}
	
	public void setPRObjects(Subject subject, PRObject prObject) {
		subjectPRData.put(subject, prObject);
	}
	
	public Set<Subject> getAllSubjects() {
		return subjectPRData.keySet();
	}
	
	public Subject lookup(String subID) {
		Iterator<Subject> subs = subjectPRData.keySet().iterator();
		for(Subject sub=subs.next();subs.hasNext();) {
			if(sub.getID().equals(subID))
				return sub;
			sub = subs.next();
		}
		return null;
	}
	
	public Object findObject(String objID) {
		Object obj = null;
		Iterator<Subject> subs = subjectPRData.keySet().iterator();
		for(Subject sub=subs.next();subs.hasNext();) {
			obj = sub.findObject(objID);
			if(obj != null)
				break;
			sub = subs.next();
		}
		
		return obj;
	}
}
